package org.example.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.pojo.DItem;
import org.example.pojo.DStaff;
import org.example.pojo.select.DItemSelect;
import org.example.service.DItemService;
import org.example.mapper.DItemMapper;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.time.*;

/**
* @author chenhongkai
* @description 针对表【d_item(宿舍用品领用表)】的数据库操作Service实现
* @createDate 2024-08-03 09:01:20
*/
@Service
public class DItemServiceImpl extends ServiceImpl<DItemMapper, DItem>
    implements DItemService{

    @Autowired
    private DItemMapper dItemMapper;

    @Override
    public Result<Map> SelectDitems(DItemSelect dItemSelect) {

        //格式化日期
        if(dItemSelect.getUpdateDate() != null && !dItemSelect.getUpdateDate().isEmpty()){
            Instant instant = Instant.parse(dItemSelect.getUpdateDate());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String formattedDateDirect = instant.atZone(ZoneId.of("Asia/Shanghai")).toLocalDate().format(formatter);
            dItemSelect.setUpdateDate(formattedDateDirect);
        }

        Page<DItem> page = new Page<>(dItemSelect.getCurrentPage(),dItemSelect.getPageSize());
        page.setSearchCount(false);

        Integer itemsTotal = dItemMapper.SelectDitemsTotal(dItemSelect);
        page.setTotal(itemsTotal);

        IPage<Map> mapIPage = dItemMapper.SelectDitems(page,dItemSelect);

        Map itemMap = new HashMap<>();
        itemMap.put("pageSize",mapIPage.getSize());
        itemMap.put("pageTotal",mapIPage.getTotal());
        itemMap.put("apartmentItem",mapIPage.getRecords());

        return Result.ok(itemMap);
    }

    @Override
    public Result<Map> UpdateItem(DItemSelect[] dItemSelects) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for(DItemSelect dItemSelect : dItemSelects) {
            DItem dItem = new DItem();
            dItem.setMid(dItemSelect.getMid());
            dItem.setItemStatus(1);
            dItem.setBackDate(sdf.parse(dItemSelect.getBackDate()));
            dItemMapper.updateById(dItem);
        }
        return Result.ok(null);
    }

    @Override
    public Result<Map> AddItem(DItemSelect[] dItemSelects) {
        for(DItemSelect dItemSelect : dItemSelects) {
            dItemMapper.insertItem(dItemSelect);
        }
        return Result.ok(null);
    }

    @Override
    public Result<Map> DeleteItem(DItemSelect[] dItemSelects) {
        for(DItemSelect dItemSelect : dItemSelects) {
            dItemMapper.deleteItem(dItemSelect);
        }
        return Result.ok(null);
    }
}




